package com.ruoyan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyan.pojo.Department;
import com.ruoyan.pojo.Employee;
import com.ruoyan.service.DepartmentService;
import com.ruoyan.service.EmployManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.ruoyan.controller
 * @ClassName: DepartmentManageController
 * @Author: ruoyan1998
 * @CreateTime: 2021/6/3 19:34
 * @Description:
 */
@Controller
public class DepartmentManageController
{
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private EmployManageService employManageService;

    /**
     * 查询所有部门*/
    @GetMapping("/department")
    public String departmentList(Model model, @RequestParam(defaultValue = "1") Integer pageNum)
    {
        PageHelper.startPage(pageNum,5);
        List<Department> departmentList = departmentService.queryDepartment();
        PageInfo<Department> pageInfo = new PageInfo<>(departmentList);

        //model.addAttribute("departmentList",departmentList);
        model.addAttribute("departmentList",departmentList);
        model.addAttribute("pageInfo",pageInfo);

        return "department/departmentList";
    }

    /**
     * 跳转到添加部门页面*/
    @GetMapping("/toAddDep")
    public String toAddDep()
    {
        return "department/add";
    }

    /**
     * 添加部门*/
    @PostMapping("/dep")
    public String addDep(Department department)
    {
        //使用try-catch判断添加操作是否能成功，若出现异常则跳转到添加异常页面，无异常则根据转发路径重定向到部门列表页面
        try
        {
            departmentService.addDepartment(department);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "department/addError";
        }

        return "redirect:/department";
    }

    /**
     * 跳转到更新部门页面*/
    @GetMapping("/dep/{id}")
    public String toUpdateDepartment(@PathVariable("id") int id, Model model)
    {
        List<Employee> employeeList = employManageService.queryEmployeeByDid(id);

        //若查询到部门下无员工信息，则此时部门表负责人字段没有与员工表关联，则只单独查询部门表信息即可
        if(employeeList.isEmpty())
        {
            Department queryDepartmentById = departmentService.queryDepartmentById(id);
            model.addAttribute("department",queryDepartmentById);
            model.addAttribute("employeeList",employeeList);

            return "department/update";
        }
        //此时部门中有员工信息，则调用根据员工表did字段查询部门信息，查询出部门中员工，以便修改负责人
        Department queryDepartmentById = departmentService.queryDepartmentByIdFindDid(id);

        model.addAttribute("department",queryDepartmentById);
        model.addAttribute("employeeList",employeeList);

        return "department/update";
    }

    /**
     * 更新部门*/
    @PostMapping("/updateDep")
    public String updateDepartment(Department department)
    {
        //若更新部门成功，则直接重定向至部门管理页面；若更新部门失败，则跳转到更新失败页面
        try
        {
            departmentService.updateDepartment(department);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "department/updateError";
        }

        return "redirect:/department";
    }

    /**
     * 删除部门*/
    @GetMapping("/deleteDep/{id}")
    public String deleteDepartment(@PathVariable("id") int id) throws Exception
    {
        int deleteDepartment = departmentService.deleteDepartment(id);

        if(0 == deleteDepartment)
        {
            return "department/deleteError";
        }

        return "redirect:/department";
    }
}
